using System;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using MCMLXVII.BackEnd.Services.Common.Messages;
using System.ServiceModel;


namespace MCMLXVII.Core.Common
{
	/// <summary>
	/// Summary description for Configuration.
	/// </summary>
	public class Configuration
	{ 

		private static readonly Configuration _instance = new Configuration();
		private XmlDocument _Cache;

		private Configuration()
		{
			_Cache = new XmlDocument();
			try
			{
                if (System.Reflection.Assembly.GetEntryAssembly() == null)
                {
                    string st = System.AppDomain.CurrentDomain.BaseDirectory ;
                    st += AppDomain.CurrentDomain.FriendlyName;
                    _Cache.Load(st + "\\Configuration\\Backend.config");
                }
                else
                {
                    string st = System.Reflection.Assembly.GetEntryAssembly().Location;
                    st = st.Substring(0, st.LastIndexOf("\\") + 1);
                    _Cache.Load(st + "SYSTEM\\Configuration\\Backend.config");
                }

			}
			catch (Exception ex)
			{
				throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical, "ConfigNotFound", ex.Message)) ;
			}
		}

		public static Configuration Instance
			{
				get{ return _instance; }		
			}

        public string GetSystemParameter(string Name)
        {
            try
            {
                return _Cache.SelectSingleNode("configuration/Parameters/Parameter[@Name='" + Name + "']").InnerText;
            }
            catch
            {
                //Tracer.Trace(ResourcesTools.RetornaLocPar(Properties.Resources.ParameterNotFound, Name ),TraceLevel.Exceptions );
                return "";
            }
        }
        public NameValueCollection GetSystemParameters()
        {
            NameValueCollection res = new NameValueCollection();
            System.Xml.XmlNodeList lst = _Cache.SelectNodes("configuration/Parameters/Parameter");
            foreach (XmlNode nod in lst)
            {
                res.Add(nod.Attributes.GetNamedItem("Name").Value, nod.InnerText);                
            }
            return res;
        }


    }
}
